<template>
  <div id="categories">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="2">
          <el-button type="primary" @click="add">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template slot="isOk" scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted == false"
            style="color:lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="isNumber" scope="scope">
          <el-tag effect="dark" size="mini" v-if="scope.row.cat_level == 0"
            >一级</el-tag
          >
          <el-tag
            effect="dark"
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag effect="dark" type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <template slot="isActive" scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="findId(scope.row.cat_id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="del(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加 -->
    <el-dialog
      title="添加商品分类"
      :visible.sync="addDialogVisible"
      width="40%"
      @close="addDialogVisibleClose"
    >
      <el-form
        :model="addRuleForm"
        :rules="addRules"
        ref="addRuleForm"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            expand-trigger="hover"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="handleChange"
            change-on-select
            clearable
            style="width:100%"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSure">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editDialogVisible"
      width="40%"
      @close="editDialogVisibleClose"
    >
      <el-form
        :model="editRuleForm"
        :rules="addRules"
        ref="editRuleForm"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editRuleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import "@/assets/css/goods/categories.scss";

export default {
  data() {
    return {
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      cateList: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isOk",
        },
        {
          label: "排序",
          type: "template",
          template: "isNumber",
        },
        {
          label: "操作",
          type: "template",
          template: "isActive",
        },
      ],
      addDialogVisible: false,
      editDialogVisible: false,
      addRuleForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      addRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      editRuleForm: {
        cat_name: "",
      },
      parentCateList: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      selectedKeys: [],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.axios.get("categories", {
        params: this.querInfo,
      });
      if (res.meta.status == 200) {
        this.cateList = res.data.result;
        this.total = res.data.total;
      } else {
        this.$message.error(res.meta.msg);
      }
    },
    handleSizeChange(val) {
      this.querInfo.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      this.querInfo.pagenum = val;
      this.getCateList();
    },

    // 添加
    add() {
      this.addDialogVisible = true;
      this.getParentCateList();
    },
    addDialogVisibleClose() {
      this.$refs.addRuleForm.resetFields();
      this.addRuleForm.cat_pid = 0;
      this.addRuleForm.cat_level = 0;
      this.selectedKeys = [];
    },
    async getParentCateList() {
      const { data: res } = await this.axios.get("categories", {
        params: { type: 2 },
      });
      if (res.meta.status == 200) {
        this.parentCateList = res.data;
      } else {
        this.$message.error(res.meta.msg);
      }
    },
    handleChange() {
      if (this.selectedKeys.length > 0) {
        this.addRuleForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ];
        this.addRuleForm.cat_level = this.selectedKeys.length;
        return;
      } else {
        this.addRuleForm.cat_pid = 0;
        this.addRuleForm.cat_level = 0;
      }
    },
    addSure() {
      this.$refs.addRuleForm.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.axios.post(
            "categories",
            this.addRuleForm
          );
          if (res.meta.status == 201) {
            this.$message.success(res.meta.msg);
            this.addDialogVisible = false;
            this.getCateList();
          } else {
            this.$message.success(res.meta.msg);
          }
        } else {
          return false;
        }
      });
    },

    // 编辑
    async findId(id) {
      this.editDialogVisible = true;
      const { data: res } = await this.axios.get(`categories/${id}`);
      if (res.meta.status == 200) {
        this.editRuleForm = res.data;
      }
    },
    editDialogVisibleClose() {
      this.$refs.editRuleForm.resetFields();
    },
    editSure() {
      this.$refs.editRuleForm.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.axios.put(
            `categories/${this.editRuleForm.cat_id}`,
            this.editRuleForm
          );
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.editDialogVisible = false;
          } else {
            this.$message.success(res.meta.msg);
          }
        } else {
          return false;
        }
        this.getCateList();
      });
    },

    // 删除
    async del(val) {
      const result = await this.$confirm(  
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (result == "confirm") {
        const { data: res } = await this.axios.delete(`categories/${val}`);
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
          this.cateList = res.data
        } else {
          this.$message.error(res.meta.msg);
        }
      } else return;
    },
  },
};
</script>

<style scoped></style>
